#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "functions.h"
#include "generator.h"
#include "depot.h"

int main(int argc, char **argv) {

	srand( (unsigned) time(NULL) );
	unsigned long int n;
	int write_to_file = 0;
	FILE *out;

	gbk_desc();

	if(check_args(argc, argv) == 1) {
		write_to_file = 1;
		n = atoi(argv[1]);
		char *fname = malloc(strlen(argv[3]));
		strcpy(fname, argv[3]);
		out = fopen(fname, "w+");
	} else if(check_args(argc, argv) == 0){
		write_to_file = 0;
		out = stdout;
		fprintf(out, "\n\nPodaj liczbe lanchuchow do wygenerowania: ");
		scanf("%ld", &n);
	} else {
		proper_using();
		exit(EXIT_SUCCESS);
	}

	init_depot(n);
	int i;	
	for(i = 0; i < n; i++) {
		str_lgt = 0;
		choose_S();
		add_lgt( str_num, str_lgt );
		str_num++;
	}
	sort_depot_lgt(n);
	sort_depot_num(n);
	print_depot(n, out);

	if(write_to_file == 1)
		fprintf(stdout, "\n\nLancuchy zostaly wygenerowane i zapisane do pliku: %s\n\n", argv[3]);
	
	return 0;
}

